//
//  AXFAddressMainController.m
//  adress
//
//  Created by 候锐 on 16/11/19.
//  Copyright © 2016年 候锐. All rights reserved.
//

#import "AXFAddressMainController.h"
#import "UIColor+CZAddition.h"
#import "AXFAddressCell.h"
#import "AXFAddressHeaderView.h"
#import "AXFAddressFooterView.h"
#import "AXFSetAddressViewController.h"
#import "AXFAddressModel.h"
#import "AXFWhiteNavView.h"

@interface AXFAddressMainController ()<UITableViewDataSource,cellDelegate,addressDelegate,AXFWhiteNavViewDelegate>
@property(nonatomic,weak) UITableView *tableView;
@end

@implementation AXFAddressMainController{
    NSMutableArray <AXFAddressModel *>*_dataArray;
    NSInteger row;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.hidden = YES;
}



- (void)AXFAddressCell:(AXFAddressCell *)cell withChangeBtn:(UIButton *)btn{
    AXFSetAddressViewController *vc = [[AXFSetAddressViewController alloc] init];

    vc.model = _dataArray[btn.tag];
    [self.navigationController pushViewController:vc animated:YES];
    
}


- (void)ClickedButtonPush{
    AXFSetAddressViewController *vc = [[AXFSetAddressViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)setupUI{
    self.navigationItem.title = @"管理收获地址";
    self.view.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    

    AXFAddressHeaderView *headerView = [[[UINib nibWithNibName:@"AXFAddressHeaderView" bundle:nil] instantiateWithOwner:nil options:nil]lastObject];
    AXFAddressFooterView *footerView = [[[UINib nibWithNibName:@"AXFAddressFooterView" bundle:nil] instantiateWithOwner:nil options:nil]lastObject];
    footerView.delegate = self;
    
    
    
    [self.view addSubview:headerView];
    [self.view addSubview:footerView];
    
    
    
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(80);
    }];
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
    
    UITableView *tv = [[UITableView alloc] init];
    self.tableView = tv;
    self.tableView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    [self.view addSubview:tv];
    
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AXFAddressCell" bundle:nil] forCellReuseIdentifier:@"addresscell"];
    self.tableView.estimatedRowHeight = 61;
    
    [tv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.bottom.equalTo(footerView.mas_top);
        make.top.equalTo(self.view).mas_offset(75);
    }];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
	AXFWhiteNavView *whiteNavView = [[AXFWhiteNavView alloc] init];
	
	[whiteNavView setTitleName:@"管理收货地址" andFunctionName:nil];
	
	whiteNavView.delegate = self;
	
	[self.view addSubview:whiteNavView];
	
	[whiteNavView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.right.top.offset(0);
		make.height.mas_equalTo(kNAVBARH + 20);
	}];
}

- (void)backToViewController {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)loadData{
    _dataArray = [NSMutableArray array];

    AXFAddressModel *model = [[AXFAddressModel alloc] init];
    model.name = @"哼哼";
    model.gender = 1;
    model.phoneNumber = @"15645999344";
    model.city = @"北京市";
    model.address = @"海淀区西三旗";
    model.detailsAdd = @"中腾建华商务大厦";
    [_dataArray addObject:model];
    
    AXFAddressModel *model2 = [[AXFAddressModel alloc] init];
    model2.name = @"哈哈";
    model.gender = 0;
    model2.phoneNumber = @"15645999344";
    model2.city = @"北京市";
    model2.address = @"昌平区回龙观";
    model2.detailsAdd = @"新龙城二期";
    [_dataArray addObject:model2];
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AXFAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addresscell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    cell.change.tag = indexPath.row;
    cell.delegate = self;
    
    
    return cell;
}
//设置组头距离
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0.1;
    }
    return 0;
}
@end
